//
//  PGDeviceTableSource.m
//  PhotographicGuide
//
//  Created by indream on 13-2-12.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGDeviceTableSource.h"
#import "PGWebViewController.h"
static PGDeviceTableSource*_instance;
@implementation PGDeviceTableSource
+(PGDeviceTableSource*)sharedSource{
    if(_instance==nil){
        _instance = [[PGDeviceTableSource alloc]init];
    }
    return _instance;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 106;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return section==0?@"5D Mark II":(section==1?@"中长焦镜头":@"望远镜头");
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 64.0;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *groupHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 64)];
    UIImageView *headerBgImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"header_bg.jpg"]];
    [headerBgImage setFrame:CGRectMake(0, 0, tableView.bounds.size.width, 64)];
    [groupHeaderView addSubview:headerBgImage];
    UIView *deviceView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 68, 44)];
    [deviceView setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:0.6]];
    [groupHeaderView addSubview:deviceView];
    UIImageView *deviceImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_camera.jpg"]];
    [deviceImage setFrame:CGRectMake(12, 12, 64, 40)];
    [groupHeaderView addSubview:deviceImage];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(84, 10, tableView.bounds.size.width-104, 20)];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithWhite:0.2 alpha:0.6];
    label.font = [UIFont boldSystemFontOfSize:14];
    [groupHeaderView addSubview:label];
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(84, 30, tableView.bounds.size.width-104, 20)];
    detailLabel.text = @"全画幅数码单反";
    detailLabel.backgroundColor = [UIColor clearColor];
    detailLabel.textColor = [UIColor colorWithWhite:0.2 alpha:0.6];
    detailLabel.font = [UIFont systemFontOfSize:12];
    [groupHeaderView addSubview:detailLabel];
    
    UIImageView *arrowImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_accessory.png"]];
    arrowImage.alpha = 0.3;
    [arrowImage setCenter:CGPointMake(304, 32)];
    [groupHeaderView addSubview:arrowImage];
    UIButton *linkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [linkButton setFrame:groupHeaderView.frame];
    [linkButton addTarget:self action:@selector(linkHandle:) forControlEvents:UIControlEventTouchUpInside];
    [groupHeaderView addSubview:linkButton];
    return groupHeaderView;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PGDeviceTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGDeviceTableCell"];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"PGDeviceTableCell" owner:self options:nil];
        cell = _tvCell;
        self.tvCell = nil;
    }
    return cell;
}
-(void)linkHandle:(id)sender{
    if(_navigationController){
        PGWebViewController *controller = [[PGWebViewController alloc]init];
        controller.title = @"5D Mark II";
        controller.url = @"http://a.m.tmall.com/i3953338960.htm?sid=198077632a9bd9a5";
        [_navigationController pushViewController:controller animated:YES];
    }
}
@end
